/**
 * 
 */
package cn.cityhouse.avm.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Formatter;

/**
 * @author <a href="mailto:wangcl@lreis.ac.cn">ChenLiang Wang</a>
 */
public class Out {

	/**
	 * (non-Javadoc) max row number for printing ,the default value is {@value}
	 */
	private static int n_max_print_row = 20;

	/**
	 * (non-Javadoc) max column number for printing,the defalut values is
	 * {@value}
	 */
	private static int n_max_print_col = 10;

	private static void changeLine(int i) {
		if (i >= Out.getN_max_print_col() && (i + 1) % Out.getN_max_print_col() == 1) {
			System.out.println();
		}
	}

	/**
	 * @return int indicating the max column number for printing
	 */
	public static int getN_max_print_col() {
		return Out.n_max_print_col;
	}

	/**
	 * @return int indicating the max row number for printing
	 */
	public static int getN_max_print_row() {
		return Out.n_max_print_row;
	}

	/**
	 * Print fucntion for double array ("|%15.5f")
	 * 
	 * @param v
	 *            the input double array to be printing
	 */
	public static void printArr(double[] v) {
		Formatter fmt = null;
		for (int i = 0; i < v.length; i++) {
			changeLine(i);
			fmt = new Formatter();
			fmt.format("|%15.5f", v[i]);
			System.out.print(fmt);
			fmt.close();
		}

	}

	/**
	 * Print function for double array
	 * 
	 * @param v
	 *            the input double array to be printing
	 * @param df
	 *            Decimal Format for printing
	 */
	public static void printArr(double[] v, DecimalFormat df) {
		for (int i = 0; i < v.length; i++) {
			changeLine(i);
			System.out.print(df.format(v[i]) + "  ");
		}

	}

	/**
	 * Print function for double array in the specified format
	 * 
	 * @param v
	 *            the input double arrary to be printing
	 * @param str_fmt
	 *            String indicating printing format
	 */
	public static void printArr(double[] v, String str_fmt) {
		Formatter fmt = null;
		for (int i = 0; i < v.length; i++) {
			changeLine(i);
			fmt = new Formatter();
			fmt.format(str_fmt, v[i]);
			System.out.print(fmt);
			fmt.close();
		}

	}

	/**
	 * Print function for String array ("%15s")
	 * 
	 * @param v
	 *            String array for printing
	 */
	public static void printArr(String[] v) {
		Formatter fmt = null;
		for (int i = 0; i < v.length; i++) {
			changeLine(i);
			fmt = new Formatter();
			fmt.format("%15s", v[i]);
			System.out.print(fmt);
		}

	}

	/**
	 * @param val
	 *            double value to be printing
	 * @param num
	 *            the retained number
	 * @return rounded value
	 */
	public static double round(double val, int num) {
		BigDecimal b = new BigDecimal(Double.toString(val));
		// double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		double f1 = b.setScale(num, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	/**
	 * This function specify the max printing column number
	 * 
	 * @param n_max_print_col
	 *            int indicating the max printing column number
	 */
	public static void setN_max_print_col(int n_max_print_col) {
		Out.n_max_print_col = n_max_print_col;
	}

	/**
	 * This function specify the max printing row number
	 * 
	 * @param n_max_print_row
	 *            int indicating the max printing row number
	 */
	public static void setN_max_print_row(int n_max_print_row) {
		Out.n_max_print_row = n_max_print_row;
	}

	private Out() {
		// TODO Auto-generated constructor stub
		throw new Error("Do not instantiating this Class");
	}

}
